![](/img/0.jpg)
Unity实现通用的信息提示框
发表于2018-11-19
评论0
2.5k浏览
想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学堂游戏程序行业精英群
711501594
在游戏里,我们也会用一些消息提示框,例如用户按了返回按钮,一般都会弹出一个确认退出的按钮。为此,这篇文章就给大家介绍下实现一个通用的信息提示框。1、创建一个信息提示框添加InfoTipsFrameScale脚本(然后将其制作为预制体)![Unity实现通用的信息提示框](http://gadimg-10045137.image.myqcloud.com/20181119/5bf2852b63b5b.png) 2、编写该信息提示框的控制脚本
/***
* Title:"智慧工厂" 项目
* 主题:全局层:提示框的动画效果
* Description:
* 功能:实现提示框的缩放功能
* Date:2018
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Global;
using kernal;
using UnityEngine.UI;
namespace View
{
public class InfoTipsFrameScale : Global_baseScalePopUp
{
private ScaleType _ScaleType = ScaleType.Scale; //缩放类型为Scale
public Button btnClose; //关闭按钮
public Text text_TipsTitle; //提示框的标题
public Text text_TipsContent; //提示框的内容
private void Start()
{
//注册相关按钮
ResigterBtn();
}
//注册按钮
///
/// 注册相关按钮
///
public void ResigterBtn()
{
if (btnClose != null)
{
EventTriggerListener.Get(btnClose.gameObject).onClick += BtnCloseMethod;
}
}
///
/// 缩放基础设置
///
public void BaseSettings()
{
//物体基础缩放设置
base.needScaleGameObject = this.gameObject.transform;
base.needScaleGameObject.gameObject.SetActive(false);
base.needScaleGameObject.localScale = new Vector3(0, 0, 0);
}
///
/// 开启缩放
///
public void StartScale()
{
this.gameObject.SetActive(true);
//物体基础缩放设置
base.ScaleMenu();
}
///
/// 关闭按钮的方法
///
///
private void BtnCloseMethod(GameObject go)
{
if (go==btnClose.gameObject)
{
//开启缩放
StartScale();
//延迟销毁物体
Destroy(this.gameObject, Global_Parameter.INTERVAL_TIME_0DOT5);
}
}
///
/// 显示提示框的标题、提示信息内容
///
/// 提示的标题
/// 提示的内容
public void DisplayTipsFrameTextContent(string TipsContents,string Tipstitle = "信息提示")
{
if (text_TipsTitle!=null&&text_TipsContent!=null)
{
text_TipsTitle.text = Tipstitle;
text_TipsContent.text = TipsContents;
}
}
}//class_end
}
/***
* Title:"智慧工厂" 项目
* 主题:全局层:信息提示框的启用与隐藏
* Description:
* 功能:实现提示信息框的加载、动画显示与隐藏(单例模式)
* Date:2018
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using kernal;
using View;
namespace Global
{
public class InfoTipsFrame
{
private static InfoTipsFrame _Instance; //本类实例
private Transform _InfoTipsFrame; //信息提示框
///
/// 本类实例
///
///
public static InfoTipsFrame GetInstance()
{
if (_Instance==null)
{
_Instance = new InfoTipsFrame();
}
return _Instance;
}
///
/// 显示信息提示框与内容
///
/// 提示的标题
/// 提示的内容
public void DisplayTipsFrameAndContents(GameObject infoTipsFrameParent, string TipsTitle, string TipsContents)
{
//获取到信息提示框且显示
GetInfoTipFrame(infoTipsFrameParent, true);
_InfoTipsFrame.GetComponent().DisplayTipsFrameTextContent(TipsContents, TipsTitle);
}
///
/// 获取到信息提示框
///
/// 信息提示框的父物体
/// 是否启用
private void GetInfoTipFrame(GameObject infoTipsFrameParent,bool IsEnable)
{
_InfoTipsFrame = LoadPrefabs.GetInstance().GetLoadPrefab("TipsFrame/TipsFrame").transform;
_InfoTipsFrame.parent = infoTipsFrameParent.transform.parent;
_InfoTipsFrame.localPosition = new Vector3(0, 0, 0);
_InfoTipsFrame.localScale = new Vector3(1, 1, 1);
_InfoTipsFrame.gameObject.SetActive(IsEnable);
if (IsEnable == true)
{
_InfoTipsFrame.GetComponent().BaseSettings();
}
_InfoTipsFrame.GetComponent().StartScale();
}
}//class_end
}
3、使用方法
/***
* Title:"XXX" 项目
* 主题:XXX
* Description:
* 功能:XXX
* Date:2017
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using Global;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SimpleUIFrame
{
public class Test_InfoTipsFrame : MonoBehaviour
{
public GameObject infoTipsFrameParent;
void Start()
{
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
//显示信息提示框及其内容
InfoTipsFrame.GetInstance().DisplayTipsFrameAndContents(infoTipsFrameParent, "信息提示", "不存在上一页数据");
}
}
}
}
将该脚本添加到一个物体上(同时禁用做好的信息提示框),运行点击键盘A即可出现该信息提示框![Unity实现通用的信息提示框](http://gadimg-10045137.image.myqcloud.com/20181119/5bf28597cf373.png) 备注:1、资源加载方法
/***
* Title:"智慧工厂" 项目
* 主题:资源加载方法
* Description:
* 功能:XXX
* Date:2018
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace kernal
{
public class LoadPrefabs
{
private static LoadPrefabs _Instance; //本脚本实例
///
/// 本类实例
///
///
public static LoadPrefabs GetInstance()
{
if (_Instance==null)
{
_Instance = new LoadPrefabs();
}
return _Instance;
}
///
/// 加载预制体
///
/// 预制体路径和名称
///
public GameObject GetLoadPrefab(string prefabsPathAndName)
{
//把资源加载到内存中
Object go = Resources.Load("Prefabs/" + prefabsPathAndName, typeof(GameObject));
//用加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载
GameObject LoadPrefab =UnityEngine.MonoBehaviour.Instantiate(go) as GameObject;
//Debug.Log("加载的预制体="+LoadPrefab);
return LoadPrefab;
}
}//class_end
}
2、通用缩放方法
/***
* Title:"医药自动化" 项目
* 主题:实现通用的物体缩放效果(父类)
* Description:
* 功能:实现物体的整体缩放、上下压缩展开、左右压缩展开动画效果
* Date:2017
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using kernal;
namespace Global
{
public class Global_baseScalePopUp : MonoBehaviour
{
protected Transform needScaleGameObject; //需要缩放的物体
protected float scaleMenuSpeed = 0.5F; //缩放的移动速度
private bool _IsScaleMark = false; //物体缩放的标识
protected ScaleType scaleType = ScaleType.None; //默认缩放的类型
public IEnumerator StartJudgeScaleType()
{
yield return new WaitForSeconds(Global_Parameter.INTERVAL_TIME_0DOT3);
switch (scaleType)
{
case ScaleType.None:
//_NeedScaleGameObject.localScale = new Vector3(1, 1, 1);
break;
case ScaleType.Scale:
needScaleGameObject.localScale = new Vector3(0, 0, 0);
break;
case ScaleType.UpAndDown:
needScaleGameObject.localScale = new Vector3(1, 0, 1);
break;
case ScaleType.LeftAndRight:
needScaleGameObject.localScale = new Vector3(0, 1, 1);
break;
default:
break;
}
}
///
/// 放大与缩小弹出菜单
///
public void ScaleMenu()
{
if (needScaleGameObject.gameObject != null)
{
if (_IsScaleMark == false)
{
needScaleGameObject.DOScale(new Vector3(1, 1, 1), scaleMenuSpeed);
_IsScaleMark = true;
}
else
{
needScaleGameObject.DOScale(new Vector3(0, 0, 0), scaleMenuSpeed);
_IsScaleMark = false;
StartCoroutine("HideGameObject");
}
}
else
{
Debug.LogError(GetType() + "/Start()/_NeedScaleGameObject " + needScaleGameObject.gameObject + " 物体不存在请检查!!!");
}
}
///
/// 上下打开弹出菜单
///
public void UpAndDown()
{
if (needScaleGameObject.gameObject != null)
{
if (_IsScaleMark == false)
{
needScaleGameObject.DOScale(new Vector3(1, 1, 1), scaleMenuSpeed);
_IsScaleMark = true;
}
else
{
needScaleGameObject.DOScale(new Vector3(1, 0, 1), scaleMenuSpeed);
_IsScaleMark = false;
StartCoroutine("HideGameObject");
}
}
else
{
Debug.LogError(GetType() + "/Start()/_NeedScaleGameObject " + needScaleGameObject.gameObject + " 物体不存在请检查!!!");
}
}
///
/// 左右打开弹出菜单
///
public void leftAndRight()
{
if (needScaleGameObject.gameObject != null)
{
if (_IsScaleMark == false)
{
needScaleGameObject.DOScale(new Vector3(1, 1, 1), scaleMenuSpeed);
_IsScaleMark = true;
}
else
{
needScaleGameObject.DOScale(new Vector3(0, 1, 1), scaleMenuSpeed);
_IsScaleMark = false;
StartCoroutine("HideGameObject");
}
}
else
{
Debug.LogError(GetType() + "/Start()/_NeedScaleGameObject " + needScaleGameObject.gameObject + " 物体不存在请检查!!!");
}
}
///
/// 隐藏游戏物体
///
IEnumerator HideGameObject()
{
yield return new WaitForSeconds(scaleMenuSpeed);
needScaleGameObject.gameObject.SetActive(false);
}
///
/// 基础面板设置
///
/// 需要缩放的物体
/// 物体缩放类型
/// 缩放的速度
public void BasePanelSettings( GameObject needScaleGo,ScaleType scaleType, float scaleSpeed=0.3F)
{
//默认隐藏右侧内容区域
if (needScaleGo != null)
{
needScaleGo.SetActive(false);
//指定弹出菜单
needScaleGameObject = needScaleGo.transform;
//指定需要弹出菜单执行的动画类型
this.scaleType = scaleType;
StartCoroutine(StartJudgeScaleType());
//物体缩放的速度
scaleMenuSpeed = scaleSpeed;
}
else
{
Log.Write(GetType() + "/BtnOnClickEvent()/使用手册面板中按钮点击对应的面板右侧内容不存在,请检查" + needScaleGo + "物体");
}
}
}//class_end
}
原文链接
著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。
如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引
标签:
UnityJavaScript游戏视觉单例模式
本文作者
Maccya彡
暂无简介
Unity导出Excel表
Unity实现相机在场景中的自由移动
Unity3D使用内部频谱分析方法做音乐视觉特效的原理及说明
Unity+Kinect骨骼绑定方法及问题解决
Unity骨骼动画优化
GWB公众号
腾讯游戏学堂公众号
|